package com.robotserver.orders.jobs.xxljob;

import com.robotserver.orders.common.DateUtil;
import com.robotserver.orders.constant.CacheKey;
import com.robotserver.orders.mapper.SysSettingMapper;
import com.robotserver.orders.param.TbOrderSyncParam;
import com.robotserver.orders.service.TbOrderService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TbOrderChannelSyncXxlJob {
    private static Logger logger = LoggerFactory.getLogger(TbOrderChannelSyncXxlJob.class);

    @Autowired
    private TbOrderService tbOrderService;
    @Autowired
    private SysSettingMapper sysSettingMapper;

    /**
     * 生产淘宝渠道订单的tokens
     * 每20秒钟执行一次拉取订单（时间范围是3分钟）
     */
    @XxlJob("pushTbOrdersChannelHander_producer")
    public void pushTbOrdersChannelHander_producer() {
        logger.info("--------TbOrderChannelSyncXxlJob pushTbOrdersChannelHander_producer------start------");

        //默认按照订单更新时间来下载
        Integer timeType = 4; // 1：按照订单淘客创建时间查询，2:按照订单淘客付款时间查询，3:按照订单淘客结算时间查询 4:按照订单更新时间
        //是否开启大促开关,开启大促，则按照订单创建时间来下载，否则则根据订单更新时间来下载
        String big_activity_flag = sysSettingMapper.getByKey("big_activity_flag");
        if(StringUtils.isNotBlank(big_activity_flag) && "1".equals(big_activity_flag)){
            timeType = 1;
        }

        //默认持续时间为35秒
        //默认持续时间为1分钟
        Long duration = 35L;
        String durationStr = "";
        try {
            //接受参数-拉取当前日期往前的几秒订单
            durationStr = XxlJobHelper.getJobParam();
            if(StringUtils.isNotBlank(durationStr)){
                //从XXL-Job接受参数
                duration = Long.parseLong(durationStr);
            }
        }catch (Exception e){
            logger.error("xxl-job para set is error {}:"+durationStr);
            //默认设置一分钟
            duration = 35L;
        }
        Integer tmptime = DateUtil.getNowTime();
        Long end_time = Long.valueOf(tmptime);                //截至时间为当前时间
        Long begin_time = tmptime - duration;                 //起始时间为前1分钟
        TbOrderSyncParam tbOrderSyncParam = new TbOrderSyncParam();
        tbOrderSyncParam.setBegin_time(begin_time);
        tbOrderSyncParam.setEnd_time(end_time);
        tbOrderSyncParam.setTimeType(timeType);              // 1：按照订单淘客创建时间查询，2:按照订单淘客付款时间查询，3:按照订单淘客结算时间查询 4:按照订单更新时间
        tbOrderSyncParam.setOrderScene(2L);                  //场景订单场景类型，1:常规订单，2:渠道订单，3:会员运营订单，默认为1
        tbOrderSyncParam.setRedisQueueKey(CacheKey.tbOrderChannelQueue);
        //调用生产者线程，遍历联盟，往redis队列中添加alimmTokens
        tbOrderService.syncOrder_producer(tbOrderSyncParam);
        logger.info("-------------往redis中生产淘宝联盟的渠道账号订单------");
    }

    /**
     * 消费淘宝联盟渠道tokens拉取渠道订单信息
     * 每30秒钟执行一次拉取订单（时间范围是3分钟）
     */
    @XxlJob("pushTbOrdersChannelHander_consumer")
    public void pushTbOrdersChannelHander_consumer() {
        logger.info("--------TbOrderChannelSyncXxlJob pushTbOrdersChannelHander_consumer------start------");
        //调用消费者线程，遍历redis队列，消费alimmTokens
        TbOrderSyncParam tbOrderSyncParam = new TbOrderSyncParam();
        tbOrderSyncParam.setRedisQueueKey(CacheKey.tbOrderChannelQueue);
        tbOrderService.syncOrder_consumer(tbOrderSyncParam);
        logger.info("---------------------------消费淘宝联盟的渠道订单-----------------");
    }

}
